package com.ruoyi.cbs.violation.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.cbs.violation.domain.vo.*;
import com.ruoyi.common.enums.project.ViolationRecordStatusEnum;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.cbs.violation.mapper.ViolationRecordMapper;
import com.ruoyi.cbs.violation.domain.ViolationRecord;
import com.ruoyi.cbs.violation.service.IViolationRecordService;

/**
 * 承包商违章记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-15
 */
@Service
public class ViolationRecordServiceImpl implements IViolationRecordService 
{
    @Autowired
    private ViolationRecordMapper violationRecordMapper;

    /**
     * 查询承包商违章记录
     * 
     * @param id 承包商违章记录主键
     * @return 承包商违章记录
     */
    @Override
    public ViolationRecordDetailRespVO selectViolationRecordById(Long id)
    {
        ViolationRecord violationRecord = violationRecordMapper.selectViolationRecordById(id);
        ViolationRecordDetailRespVO violationRecordDetailRespVO = new ViolationRecordDetailRespVO();
        BeanUtils.copyProperties(violationRecord, violationRecordDetailRespVO);
        if (violationRecord.getStatus().equals(ViolationRecordStatusEnum.CORRECTION.getCode())){
            violationRecordDetailRespVO.setStatus(ViolationRecordStatusEnum.CORRECTION.getInfo());
        }
        return violationRecordDetailRespVO;
    }

    /**
     * 查询承包商违章记录列表
     * 
     * @param violationRecord 承包商违章记录
     * @return 承包商违章记录
     */
    @Override
    public List<ViolationRecordRespVO> selectViolationRecordList(ViolationRecordReqVO violationRecord)
    {
        List<ViolationRecordRespVO> result = new ArrayList<>();
        List<ViolationRecord> violationRecords = violationRecordMapper.selectViolationRecordList(violationRecord);
        for (ViolationRecord record: violationRecords) {
            ViolationRecordRespVO violationRecordRespVO = new ViolationRecordRespVO();
            BeanUtils.copyProperties(record, violationRecordRespVO);
            if (record.getStatus().equals(ViolationRecordStatusEnum.CORRECTION.getCode())){
                violationRecordRespVO.setStatus(ViolationRecordStatusEnum.CORRECTION.getInfo());
            }
            result.add(violationRecordRespVO);
        }
        return result;
    }

    /**
     * 新增承包商违章记录
     * 
     * @param violationRecord 承包商违章记录
     * @return 结果
     */
    @Override
    public int insertViolationRecord(AddViolationRecordReqVO violationRecord)
    {
        ViolationRecord addViolationRecord = new ViolationRecord();
        BeanUtils.copyProperties(violationRecord,addViolationRecord);
        addViolationRecord.setCreateTime(DateUtils.getNowDate());
        addViolationRecord.setDelFlag("0");
        return violationRecordMapper.insertViolationRecord(addViolationRecord);
    }

    /**
     * 修改承包商违章记录
     * 
     * @param violationRecord 承包商违章记录
     * @return 结果
     */
    @Override
    public int updateViolationRecord(UpdateViolationRecordReqVO updateViolationRecordReqVO)
    {
        ViolationRecord violationRecord = violationRecordMapper.selectViolationRecordById(updateViolationRecordReqVO.getId());
        if (Objects.nonNull(violationRecord)){
            BeanUtils.copyProperties(updateViolationRecordReqVO,violationRecord);
        }
        violationRecord.setUpdateTime(DateUtils.getNowDate());
        return violationRecordMapper.updateViolationRecord(violationRecord);
    }

    /**
     * 批量删除承包商违章记录
     * 
     * @param ids 需要删除的承包商违章记录主键
     * @return 结果
     */
    @Override
    public int deleteViolationRecordByIds(Long[] ids)
    {
        return violationRecordMapper.deleteViolationRecordByIds(ids);
    }

    /**
     * 删除承包商违章记录信息
     * 
     * @param id 承包商违章记录主键
     * @return 结果
     */
    @Override
    public int deleteViolationRecordById(Long id)
    {
        return violationRecordMapper.deleteViolationRecordById(id);
    }
}
